<?php
/**
 * 페이지 이동과 관련된 함수 모음
 *
 * @package     util
 */
class utilMust
{
    public static function locationReplace($url, $msg="", $position="top")
    {
        if ($position) {
            $position = $position.'.';
        }
        echo "
                <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
                <html>
                <head>
                <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
                <script type='text/javascript'>
                    if('".$msg."' != ''){ alert('".$msg."'); }
                    ".$position."location.replace('".$url."');
                </script>
                </head>
                <body>
                </body>
                </html>
            ";
    }

    public static function windowClose()
    {
        echo "
                <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
                <html>
                <head>
                <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
                <script type='text/javascript'>
                    opener = window;
                    window.close();
                </script>
                </head>
                <body>
                </body>
                </html>
            ";
    }

    public static function selfClose($msg="")
    {
        echo "
                <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
                <html>
                <head>
                <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
                <script type='text/javascript'>
                    if('".$msg."' != ''){ alert('".$msg."'); }
                    self.close();
                </script>
                </head>
                <body>
                </body>
                </html>
            ";
    }

    public static function historyBack($msg="")
    {
        echo "
                <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
                <html>
                <head>
                <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
                <script type='text/javascript'>
                    if('".$msg."' != ''){ alert('".$msg."'); }
                    history.back();
                </script>
                </head>
                <body>
                </body>
                </html>
            ";
    }

    public static function alert($msg, $url = '', $target = '')
    {
        if ($url) {
            if ($target) {
                $target = $target.'.';
            }
            if($url == 'back') {
                $url = 'history.back(-1);';
            } else
                $url = $target.'document.location="'.$url.'";';
        }
        $sAlert = '';
        if ($msg) {
            $sAlert = "alert('".str_replace("\n", '\\n', $msg)."');";
        }
        echo "
                <!DOCTYPE html>
                <html>
                <head>
                <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
                <script type='text/javascript'>
                    $sAlert
                ".$url."
                </script>
                </head>
                <body>
                </body>
                </html>
            ";
    }

    public static function alerCallback($msg, $url = '', $value='')
    {
        if ($url) {
            $url  = 'document.callbackForm.action = "'.$url.'";';
            $url .= 'document.callbackForm.submit();';
        }else{
            $url = 'history.back(-1);';
        }

        $sAlert = '';
        if ($msg) {
            $sAlert = "alert('".str_replace("\n", '\\n', $msg)."');";
        }
        echo "
            <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
            <html>
            <head>
            <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
            <script type='text/javascript'>
                ".$sAlert."
            </script>
            </head>
            <body>
            <form name='callbackForm' id='callbackForm' method='post' action=''>
            <input type='hidden' name='callback' id='callback' value='".$value."'>
            </form>
            </body>
            </html>
            <script>
                ".$url."
            </script>
        ";
        exit;
    }

    public static function documentWrite($msg, $url = '', $target = '')
    {
        if ($url) {
            if ($target) {
                $target = $target.'.';
            }
            if($url == 'back') {
                $url = 'history.back(-1);';
            } else
                $url = $target.'document.location="'.$url.'";';
        }

        echo "
                <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
                <html>
                <head>
                <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
                <script type='text/javascript'>
                    document.write('".str_replace("\n", '\\n', $msg)."');
                ".$url."
                </script>
                </head>
                <body>
                </body>
                </html>
            ";
    }

    public static function javascript($sCommand = '')
    {
        echo "
                <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
                <html>
                <head>
                <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
                <script type='text/javascript'>
                    ".$sCommand."
                </script>
                </head>
                <body>
                </body>
                </html>
            ";
    }

    public static function redirect($url, $status = null)
    {
        if (!empty($status)) {
            header($status);
        }

        header('Location: '.$url);
        return false;
    }
}
?>